<?php
function utf8_unicode($name){
	$name = iconv('UTF-8', 'UCS-2', $name);
	$len  = strlen($name);
	$str  = '';
	for ($i = 0; $i < $len - 1; $i = $i + 2){
		$c  = $name[$i];
		$c2 = $name[$i + 1];
		if (ord($c) > 0){   //两个字节的文字
			$str .= '\u'.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
			//$str .= base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
		} else {
			$str .= '\u'.str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
			//$str .= str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
		}
	}
	$str = strtoupper($str);//转换为大写
	return $str;
}
//print_r(utf8_unicode('hao'));
function unicode_decode($name)
{
	//$name = strtolower($name);
	// 转换编码，将Unicode编码转换成可以浏览的utf-8编码
	$pattern = '/(\\\u([\w]{4}))/i';
	//$pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
	preg_match_all($pattern, $name, $matches);
	if (!empty($matches))
	{
		//$name = '';
		for ($j = 0; $j < count($matches[0]); $j++)
		{
		$str = strtolower($matches[0][$j]);
		if (strpos($str, '\\u') === 0)
		{
			$code = base_convert(substr($str, 2, 2), 16, 10);
			$code2 = base_convert(substr($str, 4), 16, 10);
			$c = chr($code).chr($code2);
			$c = iconv('UCS-2', 'UTF-8', $c);
			$name=str_replace($matches[0][$j],$c,$name);
			//$name = str_replace($matches[0],$shuzub,$message_data);
			//$name .= $c;
		}else{
			$name .= $str;
			}
		}
	}
		return $name;
}
//print_r(unicode_decode('\U0068\U0061\U006F'));
//\U0068\U0061\U006F
//'\uD83D\u'
$message_data='\U597D\U597D\uD83D\uD532\uD83D\uGH52\U597D\uD83D\uDE00';
$bgeshi= '/\\\\uD83D\\\\u(.{4,4})/';  //格式
if(preg_match_all($bgeshi,$message_data,$aa)){
	//print_r($aa);exit;
	$neirong = $aa[1];//name
	$shuzua = $aa[0];//[g:name]
	foreach ($neirong as $kk=>$vv){
		$shuzub[$kk] = '[b:'.$vv.']';
	}
	$message_data = str_replace($shuzua,$shuzub,$message_data);
	$message_data=unicode_decode($message_data);
	//print_r($message_data);
}else{
	//print_r(unicode_decode($message_data));exit;
}

//$tu=preg_replace('/\[[p]:(.*?)\]/', '', $neirong);//去掉图片
/*
PHP 写的 WebSocket 在线聊天室
使用 需要修改
./index.php
WS_STATIC_URL
WS_HOST

./server/config.php
WEBSOCKET_HOST
ADMIN_PASS  管理员登录能使用 die 结束掉 php 进程  登录 管理员 输入名称的时候 恋月,123456  123456 = 密码 然后发送die 能结束掉php 进程

Linux 需要 root 权限 运行
IIS 直接url 访问 
./server/index.php 就能开启聊天室了

*/









?>
<!DOCTYPE html>
<html dir="ltr" lang="zh" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebSocket 在线聊天室</title>
<link rel="stylesheet" href="./static/style.css" type="text/css" media="all" />
<script type="text/javascript" src="./static/jquery.js"  media="all"></script>
<script type="text/javascript" src="./static/jquery.websocket.js"  media="all"></script>
<script src="../admin/assets/js/art/artDialog.source.js?skin=opera"></script>
<script src="../admin/assets/js/art/plugins/iframeTools.source.js"></script>
<script type="text/javascript">
WS_STATIC_URL = '{$_url}/websocket/static';
WS_HOST = '120.27.199.74';
WS_PORT = 7272;

$(function(){
	var t = $('.message');
	$.wsmessage( 'msg', function( data ){
		t.append( data );
		$('.message').animate( { scrollTop: $('.message')[0].scrollHeight } ,0 );
	});
	
	$.wsmessage( 'chat', function( data ){
		t.append( data );
		$('.message').animate( { scrollTop: $('.message')[0].scrollHeight } ,0 );
	});
	
	$.wsmessage( 'name', function( data ) {
		
		if ( data ) {
			$('.msg.info.name').remove();
		}
		
	});
	
	$.wsmessage( 'list', function( data ) {
		if ( !data ) {
			return false;
		}
		$.each( data, function( k, v ){
			if ( v[1] ) {
				var w = $( '<li>' + v[0] + '</li>' ).click(function(){
					$('.send .chat').val( '@' + v[0] + ' ' );
				});
				$('.list ul').append( w );
			} else {
				$(".list ul li").each(function(){
					if ( $(this).html() == v[0] ) {
						$(this).remove();
						return false;
					}
				});
			}
		});
		$('.online').html( $('.list ul li').size() );
	});
	
	$.wsclose(function( data ){
		$(".list ul li").html('');
		$('.online').html( 0 );
		t.append( '<div class="msg info">连接已断开, 6秒后自动重试</div>' );
	});
	
	$.wsopen( function( data ) {
		t.append( '<div class="msg info">连接服务器</div>' );
		var w = t.append( '<div class="msg info name">账号:<input type="text" class="name" name="name" id="name" /><br/>密码:<input type="text" class="pass" name="pass"  /><input type="submit" class="submit" name="submit" value="确认" /></div>' );
		w.find('.submit').click(function(){
			var name = w.find('input.name').val();
			var pass = w.find('input.pass').val();
		//var room=document.getElementById('room').value;
			$.wssend('{"type":"login","name":"'+name+'","pass":"'+pass+'"}');
			//$.wssend('   echo json_encode(array('type'=>'login','name'=>'yll','id'=>2))?>');
			//$(".pass").val('');
			return false;
		});
	});
	
	$('.send .submit').click(function(){
		if ( $('.send .chat').val() ) {
			var nr=$('.send .chat').val() ;
			//var toqun=document.getElementById('toqun').value;
			//var touser=document.getElementById('touser').value;
			$.wssend('{"type":"talk","content":"'+nr+'","to_client_id":"4"}');
			/*var toren=/^@[\u4e00-\u9fa5]{2,14}:[\s]|^@[a-zA-Z0-9]{4,28}:[\s]|@[\u4e00-\u9fa5A-Za-z0-9]{3,28}:[\s]/;
			var arr= toren.test(content);
			if(arr == true){
				var nr = content.replace(toren,'');
				if(touser){
					//$.wssend('{"type":"liaot","content":"'+nr+'","to_client_id":"'+touser+'"}');
					$.wssend('{"type":"talk","content":"'+nr+'","to_client_id":"1"}');
				}else if(toqun){
					//$.wssend('{"type":"liaot","content":"'+nr+'","to_client_qid":"'+toqun+'"}');
					$.wssend('{"type":"talk","content":"'+nr+'","to_client_id":"all");
				}
			}else{
				//$.wssend('{"type":"liaot","content":"'+content+'","to_client_id":"'+touser+'"}');
				$.wssend('{"type":"talk","content":"'+nr+'","to_client_id":"all");
			}*/
			//$.wssend($.param( { chat : $('.send .chat').val() } ) );
			$('.send .chat').val('');
		}
		return false;
	});
	$('.send  .chat').keydown(function (e) {
		if ( ( e.ctrlKey && e.keyCode == 13 ) || ( e.altKey && e.keyCode == 83 ) ) {
			$('.send .submit').click();
			return false;
		}
	});
	
	$('.tool .empty').click(function(){   //清空即可
		t.html('');
	});

	$('.tool .sctp').click(function(){   //上传图片
		art.dialog.open('/?act=upfile&id=faBuWeiBo&type=3',{title:'',width:'600px',lock:true},false);
		// art.dialog.open('/user/?act=weibo&rmtype=tupian',{title:'',lock:true},false); 
	}) ;

	$('.tool .scfj').click(function(){    //上传附件
		 art.dialog.open('/user/?act=fujian&rmtype=imfjsc',{title:'',lock:true},false); 
	});
	$('.tool .scbq').click(function(){      //表情
		art.dialog.data('ys_id','faBuWeiBo');
		art.dialog.open('/?act=biaoqing',{title:'',lock:true});
	});

	$('.tool .ltjl').click(function(){    //上传聊天记录
		 art.dialog.open('/user/?act=fujian&rmtype=ltjl',{title:'',lock:true},false); 
	});
});

function wbtanchu(id){
	art.dialog.open('/user/?act=weibo&rmtype=wbtanchu&id='+id+'&size=800',{id:'wbtc',width:806,height:606,title:'',lock:true},false);
}
function zd(nicheng,uid){
	$("#faBuWeiBo").val('@'+nicheng+': ');
	var touser = document.getElementById('touser');
	var toqun= document.getElementById('toqun');
	if(toqun.value != ''){
		toqun.value ='';
	}
	touser.value = uid;	
}
function zdq(nicheng,qid){
	$("#faBuWeiBo").val('@'+nicheng+': ');
	var touser = document.getElementById('touser');
	var toqun= document.getElementById('toqun');
	if(touser.value != ''){
		touser.value ='';
	}
	toqun.value = qid;	
}

function yanse(cz){   //颜色
	var neirong = document.getElementById('faBuWeiBo').value;
	var yan=/\[c_(.*?)\]/g;   //颜色开始标签
	var jsbq=/\[\/c]/g;       //颜色结束标签
	var yanses=neirong.match(yan);
	var toren=/^@[\u4e00-\u9fa5]{2,14}:[\s]|^@[a-zA-Z0-9]{4,28}:[\s]|@[\u4e00-\u9fa5A-Za-z0-9]{3,28}:[\s]/;
	var arr= neirong.match(toren);
	if(yanses){
		nei=neirong.replace(yan,'');
		neirong=nei.replace(jsbq,'');
	}
	if(arr){
		var nr = neirong.replace(toren,'');
		if(cz){
			$("#faBuWeiBo").val(arr+'[c_'+cz+']'+nr+'[/c]');
		}else{
			$("#faBuWeiBo").val(arr+nr);
		}
	}else{
		if(cz){
			$("#faBuWeiBo").val('[c_'+cz+']'+neirong+'[/c]');
		}else{
			$("#faBuWeiBo").val(neirong);
		}
	}
}
function ziti(cz){    //字体
	var neirong = document.getElementById('faBuWeiBo').value;
	var ksbq=/\[z_(.*?)\]/g;   //字体开始标签
	var jsbq=/\[\/z]/g;       //字体结束标签
	var toren=/^@[\u4e00-\u9fa5]{2,14}:[\s]|^@[a-zA-Z0-9]{4,28}:[\s]|@[\u4e00-\u9fa5A-Za-z0-9]{3,28}:[\s]/;
	var arr= neirong.match(toren);
	var ziti=neirong.match(ksbq);
	if(ziti){
		nei=neirong.replace(ksbq,'');
		neirong=nei.replace(jsbq,'');
	}
	if(arr){
		var nr = neirong.replace(toren,'');
		if(cz){
			$("#faBuWeiBo").val(arr+'[z_'+cz+']'+nr+'[/z]');
		}else{
			$("#faBuWeiBo").val(arr+nr);
		}	
	}else{
		if(cz){
			$("#faBuWeiBo").val('[z_'+cz+']'+neirong+'[/z]');
		}else{
			$("#faBuWeiBo").val(neirong);
		}
	}
}
</script>
</head>
<body>
<div  class="left" id="left">
	<p>好友列表</p>
	<div class="haoyou"></div>
</div>
<input type="text" id="touser" value="" />
<input type="text" id="toqun" value="" />

<div class="content">
	<div class="message">
	</div>
	<div class="tool">
		<span class="empty"style="color:#0066FF;cursor:pointer">清空记录</span>
		<span class="sctp" style="color:#CC00FF;cursor:pointer">上传图片</span>
		<span class="scfj" style="color:#0066FF;cursor:pointer">上传附件</span>
		<span class="scbq" style="color:#CC00FF;cursor:pointer">表情</span>
		<div class="ziticolor">
		<span style="color: #CC00FF">对话类型:</span>
			<select name="room" style="width:80px;cursor:pointer" id="room">
                   <option value="1">一对一</option> 
                   <option value="2">一对多</option>               	
            </select>
			<span style="color: #CC00FF">字体:</span>
			<select name="ziti" style="width:80px;cursor:pointer" id="zitiid">
				   <option value="0" onClick="ziti(0)">请选择</option>
                   <option value="12" onClick="ziti(12)">12px</option> 
                   <option value="14" onClick="ziti(14)">14px</option>
                   <option value="16" onClick="ziti(20)">20px</option>
                   <option value="40" onClick="ziti(40)">40px</option>                     	
            </select>
			<span style="color: #0066FF">颜色:</span>
			<select name="yanse" style="width:80px;cursor:pointer" id="yanseid">
				   <option value="0" onClick="yanse(0)">请选择</option>
				   <option value="#000000" onClick="yanse('#000000')">黑色</option>
                   <option value="#FF0000" onClick="yanse('#FF0000')">红色</option> 
                   <option value="#0000FF" onClick="yanse('#0000FF')">蓝色</option>                  	
            </select>
            <span class="ltjl" style="color: #0066FF;cursor:pointer">查看聊天记录</span>
		</div>
		
	</div>
	<div class="send">
		<textarea class="chat" name="chat" id="faBuWeiBo"></textarea>
		<p><input type="submit" class="submit" name="submit" value="发送" /></p>
	</div>
	<div class="list">
		<h3>在线用户<strong class="online">5</strong></h3>
		<ul>
		</ul>
	</div>
</div>
<div  class="right" id="right">
	<p>群组列表</p>
	<div class="qun"></div>
</div>
</body>
</html>